import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { ElMessage } from 'element-plus';
const history = createWebHistory();
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/login/login.vue'),
  },
  {
    path: '/listView',
    name: 'ListView',
    component: () => import('../view/ListView.vue'),
    redirect: '/listView/home',
    children: [
      {
        path: 'home',
        meta: {
          title: '首页',
          requireAuth: true
        },
        component: () => import('../view/home/home.vue'),
      },
      {
        path: 'visitor',
        meta: {
          title: '访客管理',
          requireAuth: true
        },
        component: () => import('../view/visitor/visitor.vue'),
      },
      {
        path: 'vxiangqing',
        meta: {
          title: '访客管理/访客详情',
          requireAuth: true
        },
        component: () => import('../view/visitor/vxiangqing.vue'),
      },
      {
        path: 'vtianjia',
        meta: {
          title: '访客管理/添加访客',
          requireAuth: true
        },
        component: () => import('../view/visitor/vtianjia.vue'),
      },
      {
        path: 'track',
        meta: {
          title: '行程管理/轨迹管理',
          requireAuth: true
        },
        component: () => import('../view/track/track.vue'),
      },
      {
        path: '/trackadd',
        meta: {
          title: '轨迹管理/轨迹管理添加',
          requireAuth: true
        },
        component: () => import('../view/track/trackadd.vue'),
      },
      {
        path: 'auditor',
        meta: {
          title: '访客管理/核销人管理',
          requireAuth: true
        },
        component: () => import('../view/auditor/auditor.vue'),
      },


      {
        path: 'point',
        meta: {
          title: '行程管理/点位管理',
          requireAuth: true
        },
        component: () => import('../view/point/point.vue'),
      },
      {
        path: '/pointAdd',
        meta: {
          title: '点位管理添加',
          requireAuth: true
        },
        component: () => import('../view/point/pointAdd.vue'),
      },
      {
        path: '/pointupdate',
        meta: {
          title: '点位管理修改',
          requireAuth: true
        },
        component: () => import('../view/point/pointupdate.vue'),
      },
      {
        path: 'school',
        meta: {
          title: '访客管理/校园动态管理',
          requireAuth: true
        },
        component: () => import('../view/school/school.vue'),
      },
      {
        path: 'tianjia',
        meta: {
          title: '访客管理/校园动态管理/添加动态',
          requireAuth: true
        },
        component: () => import('../view/school/tianjia.vue'),
      },
      {
        path: 'sxiugai',
        meta: {
          title: '访客管理/校园动态管理/修改动态',
          requireAuth: true
        },
        component: () => import('../view/school/sxiugai.vue'),
      },
      {
        path: 'sxiangqing',
        meta: {
          title: '访客管理/校园动态管理/动态详情',
          requireAuth: true
        },
        component: () => import('../view/school/sxiangqing.vue'),
      },
      {
        path: 'journal',
        meta: {
          title: '访客管理/访客日志',
          requireAuth: true
        },
        component: () => import('../view/journal/journal.vue'),
      },
      {
        path: 'jxiangqing',
        meta: {
          title: '访客管理/访客详情',
          requireAuth:true
        },
        component: () => import('../view/journal/jxiangqing.vue'),
      },
      {
        path: 'addtianjia',
        meta: {
          title: '访客管理/核销人管理/添加核销人',
          requireAuth: true
        },
        component: () => import('../view/auditor/addtianjia.vue'),
      },
      {
        path: 'addxiugai',
        meta: {
          title: '访客管理/核销人管理/修改核销人',
          requireAuth: true
        },
        component: () => import('../view/auditor/addxiugai.vue'),
      },
      {
        path: 'cancel',
        meta: {
          title: '访客管理/核销日志',
          requireAuth: true
        },
        component: () => import('../view/cancel/cancel.vue'),
      },
      {
        path: '/settings/settings',
        name: 'settings/settings',
        meta: { title: '设置', requireAuth: true },
        children: [
          {
            path: '/settings/settings',
            name: 'settings/settings',
            component: () => import("../view/settings/settings.vue"),
            meta: { title: '系统设置', requireAuth: true },
          },
        ]
      },
      {
        path: '/charts/keyboard',
        name: 'charts/keyboard',
        meta: { title: '设置', requireAuth: true },
        children: [
          {
            path: '/charts/keyboard',
            name: 'charts/keyboard',
            component: () => import("../view/charts/keyboard.vue"),
            meta: { title: '部门设置', requireAuth: true },
          },
        ]
      },
      {
        path: '/charts/line',
        name: 'charts/line',
        meta: { title: '设置', requireAuth: true },
        children: [
          {
            path: '/charts/line',
            name: 'charts/line',
            component: () => import("../view/charts/line.vue"),
            meta: { title: '添加部门', requireAuth: true },
          },
        ]
      },
      // 权限管理
      {
        path:'/conservator',
        name:'conservator',
        component: () => import('../view/privilege/Conservator.vue'),//管理员管理
        meta:{title:'权限/管理员管理',icon:'homepage'}
      },
      {
        path:'/add-conservator',
        name:'add-conservator',
        component: () => import('../view/privilege/AddConservator.vue'),//添加管理员
        meta:{title:'权限/管理员管理/添加管理员',icon:'homepage'}
      },
      {
        path:'/detail-conservator',
        name:'detail-conservator',
        component: () => import('../view/privilege/DetailsConservator.vue'),//管理员详情
        meta:{title:'权限/管理员管理/管理员详情',icon:'homepage'}
      },
      {
        path:'/updata-conservator',
        name:'updata-conservator',
        component: () => import('../view/privilege/UpdataConservator.vue'),//管理员修改
        meta:{title:'权限/管理员管理/管理员修改',icon:'homepage'}
      },
      {
        path:'/privilege',
        name:'privilege',
        component: () => import('../view/privilege/Privilege.vue'),//角色管理
        meta:{title:'角色管理',icon:'homepage'}
      },
      {
        path:'/add-privilege',
        name:'add-privilege',
        component: () => import('../view/privilege/AddPrivilege.vue'),//添加角色>添加
        meta:{title:'添加角色',icon:'homepage'}
      },
    ],
  },
  {
    path: '/:path(.*)',
    component: () => import('../view/404/404 page.vue')
  }
];
const router = createRouter({
  history,
  routes,
});

/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token') || ''
  // 判断本地缓存有没有token ，如果有token，跳转对应的路由
  if (to.meta.requireAuth) {
    if (token) {
      next()
    } else {
      ElMessage({
        message: '请先登录',
        type: "error",
      });
      next(`/login?redirect=${to.fullPath}`)//同上
    }
  } else {
    next()
  }
})

export default router;
